#include "cinterface.h"

int CInterface::run( CBoard * board )
{
	cout << "uci" << endl;
	_messageLoop(board);
	return 0;
}

int CInterface::_messageLoop( CBoard * board )
{
	char msg[16384];	
	while( strcmp(msg,"quit") )
	{
		cin >> msg;
		
		if ( !strcmp(msg,"isready") )	cout << "readyok" << endl;
		else if ( !strcmp(msg,"t") )	_t1();
		else if ( !strcmp(msg,"go") )	_go(board);
		else if ( !strcmp(msg,"stop") )	_stop(board);
	}
	_stop(board);
	cout << "done.";
	return 0;
}

int CInterface::_t1()
{
	BITBOARD val;
	int res;
	cin >> val;
	clock_t t;
	TIMER_START(timer);
	
	// test
	//for (BITBOARD i = 0; i < 1000000000; i++ ) 
	//	POPCNT64(val, res);
		
	CList		list[1];

	T_MOVE tm;
	for ( BITBOARD bb = 0; bb < val; bb++ )
	{
		list->clear();
		
		
		tm.move = 111;
		tm.flag = 1;
		list->add( tm, 77 );
		tm.move = 222;
		tm.flag = 2;
		list->add( tm, 99 );
		tm.move = 333;
		tm.flag = 3;
		list->add( tm, 88 );
	
		while( !list->isDone() )
		{
			tm = list->get();
		}		
	}
	cout << "Move = " << tm.move << ", info = " << tm.flag <<endl;
		
	
	t = TIMER_GET(timer);
	cout << "Time = " << t << ", result = " << res << endl;
	return 0;
}

int CInterface::_go( CBoard * board )
{
	engine.StartInternalThread();
	return 0;
}

int CInterface::_stop( CBoard * board )
{
	engine.engine_case = MSG_STOP_ENGINE;
	engine.WaitForInternalThreadToExit();
	return 0;
}
